<?php

namespace App\Admin\Renderable;

use App\Models\LiveMeetingRoomUser;
use App\Models\User;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;

class LiveMeetingUser extends LazyRenderable
{

    public function render()
    {

        // 获取ID
        $id = $this->key;
        
        $data = LiveMeetingRoomUser::where('meeting_id', $id)
            ->whereIn('status', [0,1])
            ->get(['user_id','time_long','status','mike_status'])
            ->toArray();

        foreach($data as $k => $v){
            $user = User::find($v['user_id']);
            $data[$k]['user_id'] = $user ? ($user ->real_name ?: $user ->user_name) : '';
            $data[$k]['time_long'] = change_time_long($v['time_long']);
            $head_portrait = $user ? ($user ->head_portrait ? img_url($user ->head_portrait) : '') : '';

            $v['head_portrait_url'] =
            "<img data-action='preview-img' src='{$head_portrait}' style='max-width:60px;max-height:60px;cursor:pointer' class='img img-thumbnail'>";

            $data[$k]['status'] = $v['status'] ? '在线' : '下线';
            $data[$k]['mike_status'] = $v['mike_status'] ? '连麦' : '下麦';
            array_splice($data[$k],1,0,$v['head_portrait_url']);
            unset($data[$k]['user_name']);
        }

        $titles = [
            '姓名',
            '头像',
            '与会时长',
            '状态',
            '连麦状态'
        ];

        return Table::make($titles, $data) ->class('custom-data-table dataTable table');

    }
}